package cn.lbwfree.web.repository.impl;

import cn.lbwfree.web.entity.Admin;
import cn.lbwfree.web.repository.AdminRepository;
import cn.lbwfree.web.utils.JDBCTools;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminRepositoryImpl implements AdminRepository {

    @Override
    public Admin login(String username, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "Select * from bookadmin where username=? and password=?";
        PreparedStatement preparedStatement =null;
        ResultSet resultSet = null;
        Admin admin = null;
        try {
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next())
            {
                admin = new Admin(resultSet.getInt(1),resultSet.getString(2), resultSet.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return admin;

    }
}
